//
//  AlertTool.m
//  HaoHint
//
//  Created by winter on 16/5/24.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "LocalNotificationTool.h"

@implementation LocalNotificationTool

static NSMutableDictionary *_notifications = nil;

static NSString const * LocalNotificationID = @"LocalNotificationID";

+ (void)cancelLocalNotification:(NSString *)LocalNotificationID
{
    if (_notifications.count) {
        NSArray *notis = _notifications[LocalNotificationID];
        
        for (UILocalNotification *noti in notis) {
            // 将通知从系统中移除
            [[UIApplication sharedApplication] cancelLocalNotification:noti];
        }
    }
}

+ (void)scheduleLocalNotification:(LocalNotificationModel *)model
{
    NSMutableArray *notis = [NSMutableArray array];

    // 提前通知
    if (model.preSeconds > 0) {
        UILocalNotification *preNotification = [self localNotification:model];
        NSTimeInterval ti = model.seconds - model.preSeconds;
        preNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:ti];// 5秒后通知
        if (model.preSoundName && model.preSoundName.length > 0) {
            preNotification.soundName = [model.preSoundName stringByAppendingString:@".caf"];
            preNotification.alertBody = model.alertAction;
            NSLog(@"%@",model.alertAction);
        }
        [notis addObject:preNotification];
    }
    
    UILocalNotification *notification = [self localNotification:model];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:model.seconds]; // 5秒后通知
    if (model.soundName && model.soundName.length > 0) {
        notification.soundName = [model.soundName stringByAppendingString:@".caf"];
    }
    notification.alertBody = [NSString stringWithFormat:@"%@ -> 结束",model.alertBody];
    [notis addObject:notification];
    

    for (UILocalNotification *noti in notis) {
        // 将通知添加到系统中
        [[UIApplication sharedApplication] scheduleLocalNotification:noti];
    }
    
    if (!_notifications) {
        _notifications = [NSMutableDictionary dictionary];
    }
    [_notifications setObject:notis forKey:model.localNotificationID];
}

+ (UILocalNotification *)localNotification:(LocalNotificationModel *)model
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    if (notification) {
        // 设置提醒时间
        notification.timeZone = [NSTimeZone systemTimeZone];
//        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];// 5秒后通知
        
        // 设置重复通知间隔
        notification.repeatInterval = 0;
        
        // 设置提醒内容
//        notification.alertBody = model.alertBody;
//        notification.alertAction = model.alertAction;
        notification.alertAction = @"打开好提醒";
        
        // 提示音
        notification.soundName = UILocalNotificationDefaultSoundName;
        
        // 设定通知的userInfo，用来标识通知
        notification.userInfo = [model dict];
    }
    
    return notification;
}
@end

@implementation LocalNotificationModel

- (void)setPreSeconds:(NSTimeInterval)preSeconds
{
    _preSeconds = preSeconds;
    
    NSInteger minutes = preSeconds / 60;
    if (minutes >= 60) {
        self.alertAction = [NSString stringWithFormat:@"1小时后结束"];
    }
    else {
        self.alertAction = [NSString stringWithFormat:@"%ld分钟后结束",(long)minutes];
    }
}

- (NSDictionary *)dict
{
    return @{
             @"localNotificationID":self.localNotificationID,
             @"preSeconds":@(self.preSeconds),
             @"seconds":@(self.seconds)
             };
}
@end
